<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      size="small"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="采购单名称" prop="purchaseName">
        <el-input
          v-model="dataForm.purchaseName"
          placeholder="采购单名称"
        ></el-input>
      </el-form-item>

      <!-- 通过查询 sys_user 表得到 -->
      <el-form-item label="采购人" prop="assigneeName">
        <el-input
          v-model="dataForm.assigneeName"
          placeholder="采购人姓名"
        ></el-input>
      </el-form-item>

      <el-form-item label="采购仓库" prop="wareId">
        <el-select
          v-model="dataForm.wareId"
          placeholder="请选择采购仓库"
          clearable
        >
          <el-option
            :label="w.name"
            :value="w.id"
            v-for="w in wareList"
            :key="w.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系方式"></el-input>
      </el-form-item>

      <el-form-item label="优先级" prop="priority">
        <el-select clearable v-model="dataForm.priority" placeholder="优先级">
          <el-option
            :label="w.name"
            :value="w.id"
            v-for="w in priorityList"
            :key="w.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="总金额" prop="amount">
        <el-input-number
          v-model="dataForm.amount"
          controls-position="right"
          :precision="3"
          :min="0"
          label="总金额"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { warePurchaseSaveAndUpdate, wareInfoList } from "@/api/ware";
export default {
  data() {
    return {
      visible: false,
      wareList: [],
      priorityList: [
        {
          id: 1,
          name: "紧急",
        },
        {
          id: 2,
          name: "高",
        },
        {
          id: 3,
          name: "中",
        },
        {
          id: 4,
          name: "低",
        },
        {
          id: 5,
          name: "无关紧要",
        },
      ],
      dataForm: {
        id: 0,
        phone: "",
        priority: "",
        status: 0,
        wareId: "",
        amount: "",
        purchaseName: "",
        assigneeName: "",
        assigneeId: "",
      },
      dataRule: {
        phone: [
          { required: true, message: "联系方式不能为空", trigger: "blur" },
        ],
        priority: [
          { required: true, message: "优先级不能为空", trigger: "blur" },
        ],
        wareId: [
          { required: true, message: "仓库id不能为空", trigger: "blur" },
        ],
        amount: [
          { required: true, message: "总金额不能为空", trigger: "blur" },
        ],
        purchaseName: [
          { required: true, message: "采购单名称不能为空", trigger: "blur" },
        ],
        assigneeName: [
          { required: true, message: "采购单人姓名不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getWares();
  },
  methods: {
    getWares() {
      wareInfoList().then((res) => {
        if (res && res.code === 0) {
          this.wareList = res.data;
        } else {
          this.wareList = [];
        }
      });
    },
    init(data) {
      this.dataForm.id = 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (data) {
          this.dataForm.id = data.id;
          this.dataForm.phone = data.phone;
          this.dataForm.priority = data.priority;
          this.dataForm.status = data.status;
          this.dataForm.wareId = data.wareId;
          this.dataForm.amount = data.amount;
          this.dataForm.purchaseName = data.purchaseName;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          warePurchaseSaveAndUpdate({
            id: this.dataForm.id || "",
            phone: this.dataForm.phone,
            priority: this.dataForm.priority,
            wareId: this.dataForm.wareId,
            amount: this.dataForm.amount,
            purchaseName: this.dataForm.purchaseName,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
